//ZUI模态窗
function ZuiModal(opt)
{
    if( ! opt.hasOwnProperty("ajax")){ opt.ajax = "ajax"; }
    if( ! opt.hasOwnProperty("url")){ opt.url = ""; }
    if( ! opt.hasOwnProperty("title")){ opt.title = "窗口"; }
    if( ! opt.hasOwnProperty("icon")){ opt.icon = ""; }
    if( ! opt.hasOwnProperty("moveable")){ opt.moveable = true; }
    //console.log(opt);
    (new $.zui.ModalTrigger(opt)).show();
}

//关闭ZUI的模态窗
function closeZuiModal()
{
    $('#triggerModal').remove();
    $('.modal-backdrop').remove();
}

//调整容器大小，主要是后台表格用到exclude_container_height
function adjustTableSize(id, exclude_h, exclude_w, id_exclude_element)
{
    id = $.trim(id); if(id == ''){ return; }
    exclude_h = parseInt(exclude_h); if(isNaN(exclude_h)){ exclude_h = 0;}
    //如果存在排除高度容器
    id_exclude_element = $.trim(id_exclude_element);
    if(id_exclude_element.length < 1){ id_exclude_element = '_exclude_element_'; }
    var obj = $('#'+id_exclude_element);
    if(obj.length > 0){
        exclude_h += obj.outerHeight();
    }
    exclude_w = parseInt(exclude_w); if(isNaN(exclude_w)){ exclude_w = 0;}
    var box_width = $(window).width() - exclude_w;
    var box_height = $(window).height() - exclude_h;
    //console.log(box_height);
    //console.log(box_width);
    $('#'+id).css({'height':box_height, 'width':box_width, 'overflow':'scroll'});
}

//固定表头
function fixTableHead(id)
{
    document.querySelector('#'+id).addEventListener('scroll', function(e){
        this.querySelector('thead').style.transform = 'translateY('+this.scrollTop+'px)';
    });
}

//成功提示消息
function greenTip(msg)
{
    new $.zui.Messager(msg, {
        icon: 'check',
        type: 'success',
        placement: 'center'
    }).show();
}

function redTip(msg)
{
    new $.zui.Messager(msg, {
        icon: 'bell-alt',
        type: 'danger',
        placement: 'center'
    }).show();
}

//input只能输入数字 ex : onkeyup="onlyNum(this, this.value)"
function onlyNum(obj, v)
{
    obj.value = v.replace(/\D/g, '');
}

//input只能输入数字 ex : onkeyup="onlyNum(this, this.value)"
function numDot(obj, v)
{
    if(isNaN(v)){
        obj.value = '';
    }
}

//是否Email
function isEmail(str)
{
    var reg = /^([a-zA-Z0-9|\_|\-|\.])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,4}){1,2})$/;
    return reg.test(str);
}

//手机号码
function isMobile(v)
{
    if((/^1[345678]+\d{9}$/.test(v))){
        return true;
    }else{
        return false;
    }
}

function onlyMobile(obj, v)
{
    if( ! isMobile(v)){ $(obj).val(''); }
}

function isIDCard(card)
{
    //身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    return reg.test(card);
}

function onlyIDCard(obj, v)
{
    if( ! isIDCard(v)){ $(obj).val(''); }
}

//全选
function checkAll(obj, name){
    var flag = false;
    if(obj.checked == true){flag = true;}
    checkAllCheckBox(name, flag);
}

//全选
function checkAllCheckBox(name, flag)
{
    name = $.trim(name); if(name == ''){ return; }
    if(flag!== true && flag!==false){ flag = false; }
    var names = document.getElementsByName(name);
    var len = names.length;
    for(var i=0; i<len; i++){
        names[i].checked = flag;
    }
}

//是否安装flash
function checkFlash()
{
    var hasFlash = false;
    try{
        hasFlash = Boolean(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
    }catch(exception){
        hasFlash = ('undefined' != typeof navigator.mimeTypes['application/x-shockwave-flash']);
    }
    return hasFlash;
}

//提交表单
function submitFormByID(id)
{
    id = $.trim(id);
    if(id == ''){ return false; }
    $('#'+id).submit();
}

function backStep(n)
{
    n = parseInt(n);
    if(isNaN(n)){ n = -1; }
    window.history.go(n);
}

//按钮状态
function buttonStatus(id, st, wz)
{
    if(st == 'y'){
        $('#'+id).val(wz).removeClass('disabled');
    }else{
        $('#'+id).val(wz).addClass('disabled');
    }
}

function rl()
{
    window.location.reload(true);
}

function isIEBrowser()
{
    var userAgent = navigator.userAgent;
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
    if(isIE)
    {
        var IE5 = false;
        var IE55 = false;
        var IE6 = false;
        var IE7 = false;
        var IE8 = false;
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        IE55 = fIEVersion == 5.5;
        IE6 = fIEVersion == 6.0;
        IE7 = fIEVersion == 7.0;
        IE8 = fIEVersion == 8.0;
        if (IE55) {
            return 5;
        }
        if (IE6) {
            return 6;
        }
        if (IE7) {
            return 7;
        }
        if (IE8) {
            return 8;
        }
        return 9;
    }else{
        return 0;
    }
}

//JS 创建 post 表单并提交
function doPost(url, params)
{
    return doRequest(url, params, 'post');
}

//JS 创建 get 表单并提交
function doGet(url, params)
{
    return doRequest(url, params, 'get');
}

function doRequest(url, params, mtd)
{
    if( ! mtd){ mtd = "get"; }
    var temp = document.createElement("form");
    temp.action = url;
    temp.method = mtd;
    temp.style.display = "none";
    for(var x in params){
        var opt = document.createElement("input");
        opt.name = x;
        opt.value = params[x];
        temp.appendChild(opt);
    }
    document.body.appendChild(temp);
    temp.submit();
    return temp;
}

function isRadioChecked(n)
{
    var obj = document.getElementsByName(n);
    var flag = false;
    for(var i=0,j=obj.length; i<j; i++){
        if(obj[i].checked){
            flag = true;
            break;
        }
    }
    return flag;
}

//控制main_iframe前进、后退、刷新
function goStep(key)
{
    if(!key){key = 'b';}
    var obj = window.top.frames.main_iframe;
    switch(key){
        case 'b' :
            obj.history.back();
            break;
        case 'f' :
            obj.history.forward();
            break;
        case 'r' :
            obj.location.reload();
            break;
        default :
            obj.location.reload();
    }
}

//切换
function goTab(n, nn)
{
    $('.tc_'+n).hide();
    $('#tc_'+n+'_'+nn).show();
    $('.tt_'+n).removeClass('current');
    $('#tt_'+n+'_'+nn).addClass('current');
}

//mask Layer
function maskLayer(color)
{
    if(!color){color='#ffffff';}
    var tmpMask = $('<div id="maskLayer"></div>');
    tmpMask.css({
        /*'width':$(document).width()+'px',*/
        'width':'100%',
        'height':$(document).height()+'px',
        'position':'absolute',
        'top':'0px',
        'left':'0px',
        'z-index':'3000',
        'background-color':color,
        'filter':'alpha(opacity=50)',
        'opacity':'0.5'
    });
    $("body").prepend(tmpMask);
}

//mask Loading
function maskLoading(color)
{
    if(!color){color='#ffffff';}
    var tmpLoading = $('<div id="maskLoading"><p><img src="/js/libs/stretchLayer/loading.gif"></p></div>');
    tmpLoading.css({
        /*'width':$(document).width()+'px',*/
        'width':'100%',
        'height':$(document).height()+'px',
        'position':'absolute',
        'top':'0px',
        'left':'0px',
        'z-index':'3001',
        'background-color':color,
        'filter':'alpha(opacity=50)',
        'opacity':'0.5'
    });
    //var _left = ($(window).scrollLeft() + $(window).width()/2 - 16)+'px';
    var _top = ($(window).scrollTop() + $(window).height()/2 - 60)+'px';
    tmpLoading.children('p').css({"text-align":"center", "padding-top":_top});
    $("body").prepend(tmpLoading);
}

//clear mask layer
function clearMaskLayer()
{
    setTimeout("$('#maskLayer').fadeOut(500)",0);
    setTimeout("$('#maskLayer').remove()",500);
}

//clear mask layer
function clearMaskLoading()
{
    $('#maskLoading').remove();
}

function showWebNotice(n, n_title, n_body, n_url, time_out)
{
    if( ! n){ n = 1; }
    var N = 'N_' + n;
    var params = {
        tag: N,
        body: n_body,
        icon: {
            x16: '/img/om/logo_01.png',
            x32: '/img/om/logo_01.png'
        },
        requireInteraction: true,
        onClick: function(){
            n_url = $.trim(n_url);
            if(n_url != ''){
                window.open(n_url);
                Push.close(N);
            }
        }
    };
    time_out = parseInt(time_out);
    if(isNaN(time_out) || time_out>0){
        params.timeout = time_out;
    }
    Push.create(n_title, params);
}